package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.dingtalkPay.service.DingtalkPayHttpService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.dingtalkPay.pojo.dto.GetOrderInfoDTO;
import com.xbongbong.pro.dingtalkPay.pojo.dto.OrderConsumeDTO;
import com.xbongbong.pro.dingtalkPay.pojo.dto.OrderFinishDTO;
import com.xbongbong.pro.dingtalkPay.pojo.dto.SkupageGetDTO;
import com.xbongbong.pro.dingtalkPay.pojo.dto.UnfinishedOrderListDTO;
import com.xbongbong.pro.dingtalkPay.pojo.vo.GetOrderInfoVO;
import com.xbongbong.pro.dingtalkPay.pojo.vo.OrderConsumeVO;
import com.xbongbong.pro.dingtalkPay.pojo.vo.OrderFinishVO;
import com.xbongbong.pro.dingtalkPay.pojo.vo.SkupageGetVO;
import com.xbongbong.pro.dingtalkPay.pojo.vo.UnfinishedOrderListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: youli.chen
 * @date: 2021/1/29 9:56
 * @desrcption: 内购接口
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/dingTalkPay")
public class DingTalkPayController {

    @Resource
    private DingtalkPayHttpService dingtalkPayHttpService;

    /**
     *  获取内购商品SKU页面地址
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/getSkuPage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSkuPage(@RequestBody @Valid SkupageGetDTO skupageGetDTO, BindingResult br) {
        XbbResponse<SkupageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SkupageGetVO skupageGetVO = dingtalkPayHttpService.getSkuPage(skupageGetDTO);
                response = new XbbResponse<>(skupageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     *  内购商品订单处理完成
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/orderFinish", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String orderFinish(@RequestBody @Valid OrderFinishDTO orderFinishDTO, BindingResult br) {
        XbbResponse<OrderFinishVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OrderFinishVO orderFinishVO = dingtalkPayHttpService.orderFinish(orderFinishDTO);
                response = new XbbResponse<>(orderFinishVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     *  获取内购订单信息
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/getOrderInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getOrderInfo(@RequestBody @Valid GetOrderInfoDTO getOrderInfoDTO, BindingResult br) {
        XbbResponse<GetOrderInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GetOrderInfoVO getOrderInfoVO = dingtalkPayHttpService.getOrder(getOrderInfoDTO);
                response = new XbbResponse<>(getOrderInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     *  应用内购商品核销
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/orderConsume", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String orderConsume(@RequestBody @Valid OrderConsumeDTO orderConsumeDTO, BindingResult br) {
        XbbResponse<OrderConsumeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OrderConsumeVO orderConsumeVO = dingtalkPayHttpService.orderConsume(orderConsumeDTO);
                response = new XbbResponse<>(orderConsumeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     *  获取未处理的已支付订单
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/getUnfinishedOrderList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUnfinishedOrderList(@RequestBody @Valid UnfinishedOrderListDTO unfinishedOrderListDTO, BindingResult br) {
        XbbResponse<UnfinishedOrderListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UnfinishedOrderListVO unfinishedOrderListVO = dingtalkPayHttpService.getUnfinishedOrderList(unfinishedOrderListDTO);
                response = new XbbResponse<>(unfinishedOrderListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
